package org.grayrabbit.cms.publish.generate.factory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Survey;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.publish.generate.Generater;
import org.grayrabbit.cms.publish.generate.MultiGenerater;
import org.grayrabbit.cms.publish.generate.SurveyGenerater;

import freemarker.template.Configuration;

/**
 * @author Administrator
 * @version 1.0
 * @created 15-����-2011 10:43:13
 */
public class SurveyGeneraterFactory implements GeneraterFactory {
	private Configuration configuration;
	private List<Survey> surveys = new ArrayList<Survey>();
	private String rootPath;
	private Generatable generatable;
	private Admin admin;
	public SurveyGeneraterFactory(Configuration configuration, List<Survey> surveys, String rootPath,Generatable generatable,Admin admin){
		this.configuration = configuration;
		this.surveys = surveys;
		this.rootPath = rootPath;
		this.generatable = generatable;
		this.admin = admin;
	}
	
	public SurveyGeneraterFactory(Configuration configuration, Survey survey, String rootPath,Generatable generatable,Admin admin){
		this.configuration = configuration;
		this.surveys.add(survey);
		this.rootPath = rootPath;
		this.generatable = generatable;
		this.admin = admin;
	}

	public Generater create(){
		List<Generater> list = new ArrayList<Generater>();
		for (Survey survey : surveys) {
			String path = ("/"+survey.getSite().getSiteTheme()+"/default/survey/survey.tpl").substring((File.separator+rootPath).length());
			list.add(new SurveyGenerater(configuration,path,survey,generatable,admin));
		}
		if(list.size()==1)
			return list.get(0);
		return new MultiGenerater(list);
	}

}